home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / test / test_complex.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2005-10-18  |  13KB  |  355 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. import unittest
  5. import os
  6. from test import test_support
  7. import warnings
  8. warnings.filterwarnings('ignore', category = DeprecationWarning, message = '.*complex divmod.*are deprecated')
  9. from random import random
  10.  
  11. class ComplexTest(unittest.TestCase):
  12.     
  13.     def assertAlmostEqual(self, a, b):
  14.         if isinstance(a, complex):
  15.             if isinstance(b, complex):
  16.                 unittest.TestCase.assertAlmostEqual(self, a.real, b.real)
  17.                 unittest.TestCase.assertAlmostEqual(self, a.imag, b.imag)
  18.             else:
  19.                 unittest.TestCase.assertAlmostEqual(self, a.real, b)
  20.                 unittest.TestCase.assertAlmostEqual(self, a.imag, 0.0)
  21.         elif isinstance(b, complex):
  22.             unittest.TestCase.assertAlmostEqual(self, a, b.real)
  23.             unittest.TestCase.assertAlmostEqual(self, 0.0, b.imag)
  24.         else:
  25.             unittest.TestCase.assertAlmostEqual(self, a, b)
  26.  
  27.     
  28.     def assertCloseAbs(self, x, y, eps = 1.0000000000000001e-09):
  29.         '''Return true iff floats x and y "are close"'''
  30.         if abs(x) > abs(y):
  31.             x = y
  32.             y = x
  33.         
  34.         if y == 0:
  35.             return abs(x) < eps
  36.         
  37.         if x == 0:
  38.             return abs(y) < eps
  39.         
  40.         self.assert_(abs((x - y) / y) < eps)
  41.  
  42.     
  43.     def assertClose(self, x, y, eps = 1.0000000000000001e-09):
  44.         '''Return true iff complexes x and y "are close"'''
  45.         self.assertCloseAbs(x.real, y.real, eps)
  46.         self.assertCloseAbs(x.imag, y.imag, eps)
  47.  
  48.     
  49.     def assertIs(self, a, b):
  50.         self.assert_(a is b)
  51.  
  52.     
  53.     def check_div(self, x, y):
  54.         '''Compute complex z=x*y, and check that z/x==y and z/y==x.'''
  55.         z = x * y
  56.         if x != 0:
  57.             q = z / x
  58.             self.assertClose(q, y)
  59.             q = z.__div__(x)
  60.             self.assertClose(q, y)
  61.             q = z.__truediv__(x)
  62.             self.assertClose(q, y)
  63.         
  64.         if y != 0:
  65.             q = z / y
  66.             self.assertClose(q, x)
  67.             q = z.__div__(y)
  68.             self.assertClose(q, x)
  69.             q = z.__truediv__(y)
  70.             self.assertClose(q, x)
  71.         
  72.  
  73.     
  74.     def test_div(self):
  75.         simple_real = [ float(i) for i in xrange(-5, 6) ]
  76.         simple_complex = [ complex(x, y) for x in simple_real for y in simple_real ]
  77.         for x in simple_complex:
  78.             for y in simple_complex:
  79.                 self.check_div(x, y)
  80.             
  81.         
  82.         self.check_div(complex(9.9999999999999997e+199, 9.9999999999999997e+199), 1 + (0.0+0.0j))
  83.         self.check_div(complex(9.9999999999999998e-201, 9.9999999999999998e-201), 1 + (0.0+0.0j))
  84.         for i in xrange(100):
  85.             self.check_div(complex(random(), random()), complex(random(), random()))
  86.         
  87.         self.assertRaises(ZeroDivisionError, complex.__div__, 1 + (0.0+1.0j), 0 + (0.0+0.0j))
  88.  
  89.     
  90.     def test_truediv(self):
  91.         self.assertAlmostEqual(complex.__truediv__(2 + (0.0+0.0j), 1 + (0.0+1.0j)), 1 - (0.0+1.0j))
  92.         self.assertRaises(ZeroDivisionError, complex.__truediv__, 1 + (0.0+1.0j), 0 + (0.0+0.0j))
  93.  
  94.     
  95.     def test_floordiv(self):
  96.         self.assertAlmostEqual(complex.__floordiv__(3 + (0.0+0.0j), 1.5 + (0.0+0.0j)), 2)
  97.         self.assertRaises(ZeroDivisionError, complex.__floordiv__, 3 + (0.0+0.0j), 0 + (0.0+0.0j))
  98.  
  99.     
  100.     def test_coerce(self):
  101.         self.assertRaises(OverflowError, complex.__coerce__, 1 + (0.0+1.0j), 0x1L << 10000)
  102.  
  103.     
  104.     def test_richcompare(self):
  105.         self.assertRaises(OverflowError, complex.__eq__, 1 + (0.0+1.0j), 0x1L << 10000)
  106.         self.assertEqual(complex.__lt__(1 + (0.0+1.0j), None), NotImplemented)
  107.         self.assertIs(complex.__eq__(1 + (0.0+1.0j), 1 + (0.0+1.0j)), True)
  108.         self.assertIs(complex.__eq__(1 + (0.0+1.0j), 2 + (0.0+2.0j)), False)
  109.         self.assertIs(complex.__ne__(1 + (0.0+1.0j), 1 + (0.0+1.0j)), False)
  110.         self.assertIs(complex.__ne__(1 + (0.0+1.0j), 2 + (0.0+2.0j)), True)
  111.         self.assertRaises(TypeError, complex.__lt__, 1 + (0.0+1.0j), 2 + (0.0+2.0j))
  112.         self.assertRaises(TypeError, complex.__le__, 1 + (0.0+1.0j), 2 + (0.0+2.0j))
  113.         self.assertRaises(TypeError, complex.__gt__, 1 + (0.0+1.0j), 2 + (0.0+2.0j))
  114.         self.assertRaises(TypeError, complex.__ge__, 1 + (0.0+1.0j), 2 + (0.0+2.0j))
  115.  
  116.     
  117.     def test_mod(self):
  118.         self.assertRaises(ZeroDivisionError, (1 + (0.0+1.0j)).__mod__, 0 + (0.0+0.0j))
  119.         a = 3.3300000000000001 + (0.0+4.4299999999999997j)
  120.         
  121.         try:
  122.             a % 0
  123.         except ZeroDivisionError:
  124.             pass
  125.  
  126.         self.fail("modulo parama can't be 0")
  127.  
  128.     
  129.     def test_divmod(self):
  130.         self.assertRaises(ZeroDivisionError, divmod, 1 + (0.0+1.0j), 0 + (0.0+0.0j))
  131.  
  132.     
  133.     def test_pow(self):
  134.         self.assertAlmostEqual(pow(1 + (0.0+1.0j), 0 + (0.0+0.0j)), 1.0)
  135.         self.assertAlmostEqual(pow(0 + (0.0+0.0j), 2 + (0.0+0.0j)), 0.0)
  136.         self.assertRaises(ZeroDivisionError, pow, 0 + (0.0+0.0j), (0.0+1.0j))
  137.         self.assertAlmostEqual(pow((0.0+1.0j), -1), 1 / (0.0+1.0j))
  138.         self.assertAlmostEqual(pow((0.0+1.0j), 200), 1)
  139.         self.assertRaises(ValueError, pow, 1 + (0.0+1.0j), 1 + (0.0+1.0j), 1 + (0.0+1.0j))
  140.         a = 3.3300000000000001 + (0.0+4.4299999999999997j)
  141.         self.assertEqual(a ** (0.0+0.0j), 1)
  142.         self.assertEqual(a ** 0.0 + (0.0+0.0j), 1)
  143.         self.assertEqual((0.0+3.0j) ** (0.0+0.0j), 1)
  144.         self.assertEqual((0.0+3.0j) ** 0, 1)
  145.         
  146.         try:
  147.             (0.0+0.0j) ** a
  148.         except ZeroDivisionError:
  149.             pass
  150.  
  151.         self.fail('should fail 0.0 to negative or complex power')
  152.         
  153.         try:
  154.             (0.0+0.0j) ** (3 - (0.0+2.0j))
  155.         except ZeroDivisionError:
  156.             pass
  157.  
  158.         self.fail('should fail 0.0 to negative or complex power')
  159.         self.assertEqual(a ** 105, a ** 105)
  160.         self.assertEqual(a ** -105, a ** -105)
  161.         self.assertEqual(a ** -30, a ** -30)
  162.         self.assertEqual((0.0+0.0j) ** 0, 1)
  163.         b = 5.0999999999999996 + (0.0+2.2999999999999998j)
  164.         self.assertRaises(ValueError, pow, a, b, 0)
  165.  
  166.     
  167.     def test_boolcontext(self):
  168.         for i in xrange(100):
  169.             self.assert_(complex(random() + 9.9999999999999995e-07, random() + 9.9999999999999995e-07))
  170.         
  171.         self.assert_(not complex(0.0, 0.0))
  172.  
  173.     
  174.     def test_conjugate(self):
  175.         self.assertClose(complex(5.2999999999999998, 9.8000000000000007).conjugate(), 5.2999999999999998 - (0.0+9.8000000000000007j))
  176.  
  177.     
  178.     def test_constructor(self):
  179.         
  180.         class OS:
  181.             
  182.             def __init__(self, value):
  183.                 self.value = value
  184.  
  185.             
  186.             def __complex__(self):
  187.                 return self.value
  188.  
  189.  
  190.         
  191.         class NS(object):
  192.             
  193.             def __init__(self, value):
  194.                 self.value = value
  195.  
  196.             
  197.             def __complex__(self):
  198.                 return self.value
  199.  
  200.  
  201.         self.assertEqual(complex(OS(1 + (0.0+10.0j))), 1 + (0.0+10.0j))
  202.         self.assertEqual(complex(NS(1 + (0.0+10.0j))), 1 + (0.0+10.0j))
  203.         self.assertRaises(TypeError, complex, OS(None))
  204.         self.assertRaises(TypeError, complex, NS(None))
  205.         self.assertAlmostEqual(complex('1+10j'), 1 + (0.0+10.0j))
  206.         self.assertAlmostEqual(complex(10), 10 + (0.0+0.0j))
  207.         self.assertAlmostEqual(complex(10.0), 10 + (0.0+0.0j))
  208.         self.assertAlmostEqual(complex(0xAL), 10 + (0.0+0.0j))
  209.         self.assertAlmostEqual(complex(10 + (0.0+0.0j)), 10 + (0.0+0.0j))
  210.         self.assertAlmostEqual(complex(1, 10), 1 + (0.0+10.0j))
  211.         self.assertAlmostEqual(complex(1, 0xAL), 1 + (0.0+10.0j))
  212.         self.assertAlmostEqual(complex(1, 10.0), 1 + (0.0+10.0j))
  213.         self.assertAlmostEqual(complex(0x1L, 10), 1 + (0.0+10.0j))
  214.         self.assertAlmostEqual(complex(0x1L, 0xAL), 1 + (0.0+10.0j))
  215.         self.assertAlmostEqual(complex(0x1L, 10.0), 1 + (0.0+10.0j))
  216.         self.assertAlmostEqual(complex(1.0, 10), 1 + (0.0+10.0j))
  217.         self.assertAlmostEqual(complex(1.0, 0xAL), 1 + (0.0+10.0j))
  218.         self.assertAlmostEqual(complex(1.0, 10.0), 1 + (0.0+10.0j))
  219.         self.assertAlmostEqual(complex(3.1400000000000001 + (0.0+0.0j)), 3.1400000000000001 + (0.0+0.0j))
  220.         self.assertAlmostEqual(complex(3.1400000000000001), 3.1400000000000001 + (0.0+0.0j))
  221.         self.assertAlmostEqual(complex(314), 314.0 + (0.0+0.0j))
  222.         self.assertAlmostEqual(complex(0x13AL), 314.0 + (0.0+0.0j))
  223.         self.assertAlmostEqual(complex(3.1400000000000001 + (0.0+0.0j), (0.0+0.0j)), 3.1400000000000001 + (0.0+0.0j))
  224.         self.assertAlmostEqual(complex(3.1400000000000001, 0.0), 3.1400000000000001 + (0.0+0.0j))
  225.         self.assertAlmostEqual(complex(314, 0), 314.0 + (0.0+0.0j))
  226.         self.assertAlmostEqual(complex(0x13AL, 0x0L), 314.0 + (0.0+0.0j))
  227.         self.assertAlmostEqual(complex((0.0+0.0j), (0.0+3.1400000000000001j)), -3.1400000000000001 + (0.0+0.0j))
  228.         self.assertAlmostEqual(complex(0.0, (0.0+3.1400000000000001j)), -3.1400000000000001 + (0.0+0.0j))
  229.         self.assertAlmostEqual(complex((0.0+0.0j), 3.1400000000000001), (0.0+3.1400000000000001j))
  230.         self.assertAlmostEqual(complex(0.0, 3.1400000000000001), (0.0+3.1400000000000001j))
  231.         self.assertAlmostEqual(complex('1'), 1 + (0.0+0.0j))
  232.         self.assertAlmostEqual(complex('1j'), (0.0+1.0j))
  233.         self.assertAlmostEqual(complex(), 0)
  234.         self.assertAlmostEqual(complex('-1'), -1)
  235.         self.assertAlmostEqual(complex('+1'), 1)
  236.         
  237.         class complex2(complex):
  238.             pass
  239.  
  240.         self.assertAlmostEqual(complex(complex2(1 + (0.0+1.0j))), 1 + (0.0+1.0j))
  241.         self.assertAlmostEqual(complex(real = 17, imag = 23), 17 + (0.0+23.0j))
  242.         self.assertAlmostEqual(complex(real = 17 + (0.0+23.0j)), 17 + (0.0+23.0j))
  243.         self.assertAlmostEqual(complex(real = 17 + (0.0+23.0j), imag = 23), 17 + (0.0+46.0j))
  244.         self.assertAlmostEqual(complex(real = 1 + (0.0+2.0j), imag = 3 + (0.0+4.0j)), -3 + (0.0+5.0j))
  245.         c = 3.1400000000000001 + (0.0+1.0j)
  246.         self.assert_(complex(c) is c)
  247.         del c
  248.         self.assertRaises(TypeError, complex, '1', '1')
  249.         self.assertRaises(TypeError, complex, 1, '1')
  250.         self.assertEqual(complex('  3.14+J  '), 3.1400000000000001 + (0.0+1.0j))
  251.         if test_support.have_unicode:
  252.             self.assertEqual(complex(unicode('  3.14+J  ')), 3.1400000000000001 + (0.0+1.0j))
  253.         
  254.         self.assertRaises(ValueError, complex, '1+1j\x00j')
  255.         self.assertRaises(TypeError, int, 5 + (0.0+3.0j))
  256.         self.assertRaises(TypeError, long, 5 + (0.0+3.0j))
  257.         self.assertRaises(TypeError, float, 5 + (0.0+3.0j))
  258.         self.assertRaises(ValueError, complex, '')
  259.         self.assertRaises(TypeError, complex, None)
  260.         self.assertRaises(ValueError, complex, '\x00')
  261.         self.assertRaises(TypeError, complex, '1', '2')
  262.         self.assertRaises(TypeError, complex, '1', 42)
  263.         self.assertRaises(TypeError, complex, 1, '2')
  264.         self.assertRaises(ValueError, complex, '1+')
  265.         self.assertRaises(ValueError, complex, '1+1j+1j')
  266.         self.assertRaises(ValueError, complex, '--')
  267.         if test_support.have_unicode:
  268.             self.assertRaises(ValueError, complex, unicode('1' * 500))
  269.             self.assertRaises(ValueError, complex, unicode('x'))
  270.         
  271.         
  272.         class EvilExc(Exception):
  273.             pass
  274.  
  275.         
  276.         class evilcomplex:
  277.             
  278.             def __complex__(self):
  279.                 raise EvilExc
  280.  
  281.  
  282.         self.assertRaises(EvilExc, complex, evilcomplex())
  283.         
  284.         class float2:
  285.             
  286.             def __init__(self, value):
  287.                 self.value = value
  288.  
  289.             
  290.             def __float__(self):
  291.                 return self.value
  292.  
  293.  
  294.         self.assertAlmostEqual(complex(float2(42.0)), 42)
  295.         self.assertAlmostEqual(complex(real = float2(17.0), imag = float2(23.0)), 17 + (0.0+23.0j))
  296.         self.assertRaises(TypeError, complex, float2(None))
  297.  
  298.     
  299.     def test_hash(self):
  300.         for x in xrange(-30, 30):
  301.             self.assertEqual(hash(x), hash(complex(x, 0)))
  302.             x /= 3.0
  303.             self.assertEqual(hash(x), hash(complex(x, 0.0)))
  304.         
  305.  
  306.     
  307.     def test_abs(self):
  308.         nums = [ complex(x / 3.0, y / 7.0) for x in xrange(-9, 9) for y in xrange(-9, 9) ]
  309.         for num in nums:
  310.             self.assertAlmostEqual((num.real ** 2 + num.imag ** 2) ** 0.5, abs(num))
  311.         
  312.  
  313.     
  314.     def test_repr(self):
  315.         self.assertEqual(repr(1 + (0.0+6.0j)), '(1+6j)')
  316.         self.assertEqual(repr(1 - (0.0+6.0j)), '(1-6j)')
  317.         self.assertNotEqual(repr(-(1 + (0.0+0.0j))), '(-1+-0j)')
  318.  
  319.     
  320.     def test_neg(self):
  321.         self.assertEqual(-(1 + (0.0+6.0j)), -1 - (0.0+6.0j))
  322.  
  323.     
  324.     def test_file(self):
  325.         a = 3.3300000000000001 + (0.0+4.4299999999999997j)
  326.         b = 5.0999999999999996 + (0.0+2.2999999999999998j)
  327.         fo = None
  328.         
  329.         try:
  330.             fo = open(test_support.TESTFN, 'wb')
  331.             print >>fo, a, b
  332.             fo.close()
  333.             fo = open(test_support.TESTFN, 'rb')
  334.             self.assertEqual(fo.read(), '%s %s\n' % (a, b))
  335.         finally:
  336.             if fo is not None and not (fo.closed):
  337.                 fo.close()
  338.             
  339.             
  340.             try:
  341.                 os.remove(test_support.TESTFN)
  342.             except (OSError, IOError):
  343.                 pass
  344.  
  345.  
  346.  
  347.  
  348.  
  349. def test_main():
  350.     test_support.run_unittest(ComplexTest)
  351.  
  352. if __name__ == '__main__':
  353.     test_main()
  354.  
  355.